var base = null;
$(function(e){
	base = new Base64();
	dataInit();
});

function dataInit(){
	eventInit();
	$('#messageTable').bootstrapTable({
		url: path+'a9a098425c17412d95895839573e1a8e', method: 'post', striped: true, cache: false, pagination: true,
        contentType: "application/x-www-form-urlencoded", sidePagination: "server", pageSize: 10, uniqueId: "id", detailView: false,
        queryParams: function (params) {
        	return { limit: params.limit, offset: params.offset, searchParams : $("#searchParams").val()};
        },//传递参数（*）
        columns:[{field: 'Number', title: '序号', align: 'center', width: '50', formatter: function (value, row, index) {
        			return index+1;
        		}}, 
        		{field: 'openid', title: '用户标识', align: 'center', width: '150', formatter: function (value, row, index) {
        			if(isNull(value)){
	            		value = '-';
	            	}
        			return '<a style="word-wrap:break-word;">'+value+'</a>';
	            }},
	            {field: 'nickname', title: '昵称', align: 'center', width: '150', formatter: function (value, row, index) {
	            	if(isNull(value)){
	            		value = '-';
	            	}
	            	return '<a style="word-wrap:break-word;">'+base.decode(value)+'</a>';
	            }},
	            {field: 'sex', title: '性别', align: 'center', width: '150', formatter: function (value, row, index) {
	            	if(value==1){
	            		return '<a style="word-wrap:break-word;color:green">男</a>';
	            	}else if(value==2){
	            		return '<a style="word-wrap:break-word;color:red">女</a>';
	            	}else if(value==0){
	            		return '<a style="word-wrap:break-word;color:blue">未知</a>';
	            	}else if(isNull(value)){
	            		return '<a style="word-wrap:break-word;">-</a>';
	            	}
	            }},
	            {field: 'subscribe', title: '是否订阅', align: 'center', width: '150', formatter: function (value, row, index) {
	            	if(value==0){
	            		return '<a style="word-wrap:break-word;color:red">未订阅</a>';
	            	}else if(value==1){
	            		return '<a style="word-wrap:break-word;color:green">已订阅</a>';
	            	}else if(isNull(value)){
	            		return '<a style="word-wrap:break-word;">-</a>';
	            	}
	            }},
	            {field: 'family_id', title: '是否绑定用户', align: 'center', width: '150', formatter: function (value, row, index) {
	            	if(value == 0){
	            		return '<a style="word-wrap:break-word;color:red">未绑定</a>';
	            	} else {
	            		return '<a style="word-wrap:break-word;color:green">已绑定</a>';
	            	}
	            }},
	            {field: 'headimgurl', title: '用户头像', align: 'center', width: '150', formatter: function (value, row, index) {
	            	if(isNull(value)){
	            		value = '-';
	            	}
	            	return '<img class="img-circle" style="width:40px;height:40px;" src="'+value+'"/>';
	            }},
	            {field: 'operate', title: '操作', align: 'center', width: '210', events: {
	                'click .RoleOfA': function (e, value, row, index) {
	                	qiao.bs.confirm("确定要删除此项吗？",function(){
	                		deleteWechatUser(row.id);
		                },function(){});
	                },
	            },
	            formatter: function (value, row, index) {
	            	return [
	        	        '<button type="button" class="RoleOfA btn btn-default  btn-sm" style="margin-right:15px;">删除</button>',
	    	        ].join('');
	            }
        }],
        onLoadSuccess: function(data){  //加载成功时执行
        	if(data.returnCode!=0){
        		qiao.bs.msg({msg:data.returnMessage,type:'danger'});
        	}
        }
    });
}

function eventInit(){
	//更新微信用户，往数据库中插入数据
	$('body').on('click', '#refreshDiv', function(e){
		AjaxPostUtil.request({url : path+"e3255e8085264c258367424c70d2ee62", params : {}, type : 'json', callback : function(jsonMation){
    		if(jsonMation.returnCode == 0){
    			qiao.bs.msg({msg : "更新成功.", type : 'success'});
    			$('#messageTable').bootstrapTable('refresh',{url : path+'a9a098425c17412d95895839573e1a8e'});
    		}else{
    			qiao.bs.msg({msg : "参数错误.", type : 'danger'});
    		}
    	}});
	});
	
	//回车模糊搜索
	$('body').keydown(function() {
        if (event.keyCode == "13") {//keyCode=13是回车键
        	var params = {
        			limit : 10,
        			offset : 0,
        			searchParams : $("#searchParams").val()
        	}
        	AjaxPostUtil.request({url : path+"a9a098425c17412d95895839573e1a8e", params : params, type : 'json', callback : function(json){
        		if(json.returnCode == 0){
        			console.log(json);
        			$('#messageTable').bootstrapTable('refresh',{url : path+'a9a098425c17412d95895839573e1a8e'});
        		}else{
        			qiao.bs.msg({msg : '参数错误.', type : 'danger'});
        		}
        	}});
        }
	});
}

function deleteWechatUser(rowId){
	AjaxPostUtil.request({url : path+"35e9bc16bbf143b5822ce35650416306", params : {rowId : rowId}, type : 'json', callback : function(jsonMation){
		if(jsonMation.returnCode == 0){
			qiao.bs.msg({msg : "删除成功！", type : 'success'});
			$('#messageTable').bootstrapTable('refresh',{url : path+'a9a098425c17412d95895839573e1a8e'});
		}else{
			qiao.bs.msg({msg : "删除失败！", type : 'danger'});
		}
	}});
}